import _ from 'underscore'
import moment from 'moment'
import dispatcher from 'lib/dispatcher'
import $ from 'jquery'
import {} from 'jquery.cookie'

class DateRangeStore {
  constructor() {
    this.state = {
      days: [
        {
          value: moment.duration(1, 'days').valueOf(),
          text: '1 天'
        },
        {
          value: moment.duration(3, 'days').valueOf(),
          text: '3 天'
        },
        {
          value: moment.duration(7, 'days').valueOf(),
          text: '7 天'
        },
        {
          value: moment.duration(15, 'days').valueOf(),
          text: '15 天'
        },
        {
          value: moment.duration(30, 'days').valueOf(),
          text: '30 天'
        }
      ],
      minDate: moment().startOf('day').subtract(3, 'months').valueOf(),
      maxDate: moment().startOf('day').valueOf(),
      isOpen: false,
      isCustom: false
    };

    let spanInCookie = $.cookie('spanTime1',Number)
      , endInCookie = $.cookie('endTime1',Number);
    if (spanInCookie) {
      this.state.value = {
        spanTime: spanInCookie,
        endTime: endInCookie
      };
    } else {
      this.state.value = {
        spanTime: moment.duration(7, 'days').valueOf(),
        endTime: moment().startOf('day').add(1, 'days').valueOf()
      };
    }

    this.setDisplayText(this.state.value);

    this.state.leftMaxDate = this.state.maxDate;
    this.state.leftSelectDate = this.state.minDate;
    this.state.rightMinDate = this.state.minDate;
    this.state.rightSelectDate = this.state.maxDate;

    this.bind = this.bind.bind(this);
    this.unbind = this.unbind.bind(this);
    this.getValue = this.getValue.bind(this);
    this.getDisplayText = this.getDisplayText.bind(this);
    this.setDefaultItem = this.setDefaultItem.bind(this);
  }

  getValue() {
    return this.state.value;
  }

  getDisplayText() {
    return this.state.displayText;
  }

  setDefaultItem(defaultItem, isCustom = false) {
    if (defaultItem) {
      this.state.value = defaultItem;
      this.setDisplayText(this.state.value);
    }
  }

  setDisplayText(value) {
    let beginTime = moment(value.endTime - value.spanTime).format('YYYY-MM-DD');
    let endTime = moment(value.endTime).subtract(1, 'days').format('YYYY-MM-DD');
    this.state.displayText = beginTime + ' ~ ' + endTime;

    var option = {path:'/'}
    if(document.domain.indexOf(BlueWare.siteDomain)>-1){
      option.domain = BlueWare.siteDomain;
    }

    $.cookie('spanTime1',value.spanTime,option);
    $.cookie('endTime1',value.endTime,option);

    if(this.emit) {
      this.emit();
    }
  }

  loaded() {
    this.__register = dispatcher.register((payload) => {
      var action = this['$' + payload.$type.toLowerCase()];
      if (action) {
        action.call(this, payload);
      }
    });
  }

  bind(view) {
    this.emit = function (target, callback) {
      if (!this.__unbinded) {
        if(callback) {
          target.setState(this.state, callback);
        } else {
          target.setState(this.state);
        }
      }
    }.bind(this, view);

    this.view = view;
    this.loaded();
    this.emit();
  }

  unbind() {
    if (this.__register) {
      dispatcher.dispatch(this.__register);
    }

    this.__unbinded = true;

    if (this.willUnload) {
      this.willUnload();
    }
  }

  $state_changed(payload) {
    _.extend(this.state, payload.data);
    this.emit();
  }

  $select_item(payload) {
    _.extend(this.state, payload.data);
    this.setDisplayText(this.state.value);
    this.emit(() => {
      let action = {
        isCustom: this.state.isCustom,
        spanTime: this.state.value.spanTime,
        endTime: this.state.value.endTime
      };

      dispatcher.dispatch({
        $type: 'set_date_range',
        data: action
      });

      dispatcher.dispatch({
        $type: 'SETTIME',
        data: action
      });
    });
  }
}

export default new DateRangeStore()
